package cn.jiangxiong.pethome.system.vcode.controller;

import cn.jiangxiong.pethome.constants.BaseConstants;
import cn.jiangxiong.pethome.system.vcode.domain.Vcode;
import cn.jiangxiong.pethome.utils.AjaxResult;
import cn.jiangxiong.pethome.utils.StrUtils;
import cn.jiangxiong.pethome.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vcode")
@Api(value = "验证码的API",description="验证码的功能")
public class CodeController {
    @Value("${msg_Uid}")
    private String msg_Uid;
    @Value("${msg_Key}")
    private String msg_Key;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/imgCode/{key}")
    public AjaxResult getImgCode(@PathVariable String key){
        try {
            String code = StrUtils.getRandomString(6).toLowerCase();
            redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);
            String base64Img = VerifyCodeUtils.VerifyCode(110, 40, code);
            return AjaxResult.success().setData(base64Img);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请联系管理员！！！");
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody Vcode vcode){
        String imageCodeKey = vcode.getImageCodeKey();
        String imageCode = vcode.getImageCode();
        String phone = vcode.getPhone();
        //参数校验
        if (imageCodeKey == null || "".equals(imageCodeKey.trim())){
            return AjaxResult.error().setMsg("非法访问！");
        }
        if (phone == null || "".equals(phone.trim())){
            return AjaxResult.error().setMsg("手机号不能为空!");
        }
        if (imageCode == null || "".equals(imageCode.trim())){
            return AjaxResult.error().setMsg("验证码不能为空!");
        }
        //前置条件
        //验证码校验
        Object value = redisTemplate.opsForValue().get(imageCodeKey);
        if (value == null){
            //校验验证码是否失效
            return AjaxResult.error().setMsg("图片验证码已失效，请刷新图片。");
        }
        if (!value.toString().equals(imageCode)){
            //判断用户输入的验证码是否正确
            return AjaxResult.error().setMsg("验证码输入错误");
        }
        //手机校验要自己做
        String phoneCode = StrUtils.getRandomString(4).toLowerCase();

        String keyTime = phone + BaseConstants.Vcode.REGISTER_TIME;
        //防止短信重发
        //取时间
        Object o = redisTemplate.opsForValue().get(keyTime);
        if (o == null){
            //添加业务键标识，区别Key的不同
            long startTime = System.currentTimeMillis();
            String key = phone + BaseConstants.Vcode.REGISTER;
            redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.Vcode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.Vcode.REGISTER_TIME_OUT,TimeUnit.MINUTES);

        }else {
        Long aLong = Long.valueOf(o.toString());
        long endTime = System.currentTimeMillis();


        if (endTime-aLong<60000){
            return AjaxResult.error().setMsg("请勿重复获取验证码");

        }else {
            //添加业务键标识，区别Key的不同
            String key = phone +BaseConstants.Vcode.REGISTER;
            //重置时间
            phoneCode = (String) redisTemplate.opsForValue().get(key);
            long startTime = System.currentTimeMillis();
            redisTemplate.opsForValue().set(key, phoneCode, BaseConstants.Vcode.REGISTER_TIME_OUT, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(keyTime, startTime, BaseConstants.Vcode.REGISTER_TIME_OUT, TimeUnit.MINUTES);

            }

        }






        //发送短信验证码
        /*SendMsgUtils.sendMsg(msg_Uid,msg_Key,phone,phoneCode);*/
        //把短信验证码打印出来err表示打印红色
        System.err.println(phoneCode);

         return AjaxResult.success().setMsg("短信发送成功");
    }

}
